﻿using Common.Models;
using Core.Entities.SM;
using Core.Entities.Stock;
using Core.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Infrastructure.Services.SM
{
    /// <summary>
    /// 入库记录服务
    /// </summary>
    public class StockInService : BaseService<StockIn, string>
    {
        public StockInService(
            IBaseRepository<StockIn, string> repository,
            IPagedRepository<StockIn, string> pagedRepository)
            : base(repository, pagedRepository)
        {
        }

        /// <summary>
        /// 根据物料编码查询入库记录
        /// </summary>
        /// <param name="materCode">物料编码</param>
        /// <returns>入库记录列表</returns>
        public async Task<IEnumerable<StockIn>> GetByMaterCodeAsync(string materCode)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "MaterCode", Operator = "=", Value = materCode },
                    new FilterCondition { Field = "IsDeleted", Operator = "=", Value = false }
                }
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }

        /// <summary>
        /// 根据入库批次号查询入库记录
        /// </summary>
        /// <param name="batchCode">批次号</param>
        /// <returns>入库记录列表</returns>
        public async Task<IEnumerable<StockIn>> GetByBatchCodeAsync(string batchCode)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "In_BatchCode", Operator = "=", Value = batchCode },
                    new FilterCondition { Field = "IsDeleted", Operator = "=", Value = false }
                }
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }

        /// <summary>
        /// 根据仓库ID查询入库记录
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns>入库记录列表</returns>
        public async Task<IEnumerable<StockIn>> GetByWarehouseIdAsync(string warehouseId)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "WarehouseID", Operator = "=", Value = warehouseId },
                    new FilterCondition { Field = "IsDeleted", Operator = "=", Value = false }
                }
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }

        /// <summary>
        /// 根据筒仓编码查询入库记录
        /// </summary>
        /// <param name="siloCode">筒仓编码</param>
        /// <returns>入库记录列表</returns>
        public async Task<IEnumerable<StockIn>> GetBySiloCodeAsync(string siloCode)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "SiloCode", Operator = "=", Value = siloCode },
                    new FilterCondition { Field = "IsDeleted", Operator = "=", Value = false }
                }
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }

        public class StockInQueryDto
        {
            public QueryFilter Filter { get; set; }
            public StockIn StockIn { get; set; }
        }

        /// <summary>
        /// 高级分页查询入库记录
        /// </summary>
        /// <param name="filter">分页过滤器</param>
        /// <param name="stockIn">查询条件</param>
        /// <returns>分页结果</returns>
        public async Task<PagedResult<StockIn>> AdvancedPagedQueryAsync(QueryFilter filter, StockIn stockIn)
        {
            filter.Conditions = filter.Conditions ?? new List<FilterCondition>();

            // 添加物料编码查询条件
            if (!string.IsNullOrEmpty(stockIn.MaterCode))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "MaterCode",
                    Operator = "=",
                    Value = stockIn.MaterCode
                });
            }

            // 添加物料名称查询条件
            if (!string.IsNullOrEmpty(stockIn.MaterName))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "MaterName",
                    Operator = "=", // 不使用模糊查询
                    Value = stockIn.MaterName
                });
            }

            // 添加入库批次号查询条件
            if (!string.IsNullOrEmpty(stockIn.In_BatchCode))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "In_BatchCode",
                    Operator = "=",
                    Value = stockIn.In_BatchCode
                });
            }

            // 添加仓库ID查询条件
            if (!string.IsNullOrEmpty(stockIn.WarehouseID))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "WarehouseID",
                    Operator = "=",
                    Value = stockIn.WarehouseID
                });
            }

            // 添加入库类型查询条件
            if (!string.IsNullOrEmpty(stockIn.In_Type))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "In_Type",
                    Operator = "=",
                    Value = stockIn.In_Type
                });
            }

            // 添加入库时间范围查询
            if (stockIn.In_Time.HasValue)
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "In_Time",
                    Operator = ">=",
                    Value = stockIn.In_Time.Value
                });
            }

            // 添加入库数量查询
            if (stockIn.In_Quantity.HasValue)
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "In_Quantity",
                    Operator = ">=",
                    Value = stockIn.In_Quantity.Value
                });
            }
            // 添加时间范围查询条件
            if (!string.IsNullOrEmpty(stockIn.InTimeScope))
            {
                string[] dates = stockIn.InTimeScope.Split('至');
                DateTime startDate = DateTime.Parse(dates[0].Trim());
                DateTime endDate = DateTime.Parse(dates[1].Trim());
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "In_Time",
                    Operator = ">=",
                    Value = startDate
                });
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "In_Time",
                    Operator = "<=",
                    Value = endDate
                });
            }
            // 添加软删除过滤条件
            filter.Conditions.Add(new FilterCondition
            {
                Field = "IsDeleted",
                Operator = "=",
                Value = false
            });

            return await _pagedRepository.GetPagedAsync(filter);
        }

        /// <summary>
        /// 软删除入库记录
        /// </summary>
        /// <param name="id">入库记录ID</param>
        public async Task SoftDeleteAsync(string id)
        {
            var record = await _repository.GetByIdAsync(id);
            if (record != null)
            {
                record.IsDeleted = true;
                await _repository.UpdateAsync(record);
            }
        }
    }
}